package com.spa.application.service.admin;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.spa.application.dto.admin.RankingListDTO;
import com.spa.domain.entity.CitiesServed;
import com.spa.domain.entity.OrderInfo;
import com.spa.domain.entity.Shop;
import com.spa.domain.service.CitiesServedService;
import com.spa.domain.service.OrderService;
import com.spa.domain.service.ShopService;
import com.spa.domain.service.UserInfoService;
import com.spa.infrastructure.enums.dict.order.OrderStatusEnum;
import com.spa.infrastructure.repository.condition.CitiesServedCondition;
import com.spa.infrastructure.repository.condition.OrderInfoCondition;
import com.spa.infrastructure.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DataScreenApplicationService {

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private OrderService orderService;

    @Resource
    private CitiesServedService citiesServedService;

    @Resource
    private ShopService shopService;

    public void index() {
        long userNumbers = userInfoService.countAllUser();
        OrderInfoCondition orderInfoCondition = new OrderInfoCondition();
        orderInfoCondition.setStatus(OrderStatusEnum.$40.getCode());
        long orderCompleteNumbers = orderService.countByCondition(orderInfoCondition);
        orderInfoCondition.setStatus(OrderStatusEnum.$30.getCode());
        long ingOrderNumbers = orderService.countByCondition(orderInfoCondition);
        long orderUserNumbers = orderService.countDistinctUser4ByStatus(OrderStatusEnum.$40.getCode());
        long ingOrderUserNumbers = orderService.countDistinctUser4ByStatus(OrderStatusEnum.$30.getCode());
        List<OrderInfo> orderInfos = orderService.listGroupByUser(OrderStatusEnum.$40.getCode());
        log.info("平台用户总数：{}，订单完成总数： {}，进行中的订单： {}，下单用户数： {}，进行中的用户数： {}", userNumbers, orderCompleteNumbers, ingOrderNumbers, orderUserNumbers, ingOrderUserNumbers);
        orderInfos.forEach(orderInfo -> {
            log.info("用户：{}，完成订单数： {}，消费总金额： {}", orderInfo.getUserId(), orderInfo.getOrderCount(), orderInfo.getTotalAmount());
        });
    }

    public List<RankingListDTO> getRankingList() {
        List<RankingListDTO> rankingList = new ArrayList<>();

        //目前在运营的城市
        CitiesServedCondition citiesServedCondition = new CitiesServedCondition();
        citiesServedCondition.setStatus(10);
        List<CitiesServed> citiesServedList = citiesServedService.listInfo(citiesServedCondition);

        citiesServedList.forEach(citiesServed -> {
            //查询当月的加钟数和业绩
            LocalDate currentMonth = LocalDate.now();
            String beginDate =
                    DateUtil.date2Str(DateUtil.getStartTimeOfCurrentMonth(DateUtil.localDate2Date(currentMonth)));
            String endDate =
                    DateUtil.date2Str(DateUtil.getEndTimeOfCurrentMonth(DateUtil.localDate2Date(currentMonth)));
            List<Shop> shop = shopService.getShopByCitiesId(citiesServed.getId());
            OrderInfoCondition condition = new OrderInfoCondition();
            condition.setBeginDate(beginDate);
            condition.setEndDate(endDate);
            if (CollUtil.isNotEmpty(shop)) {
                condition.setShopIds(shop.stream().map(Shop::getId).toList());
            }
            List<OrderInfo> orderInfoList = orderService.orderList(condition);
            int totalPrice = 0;
            int extTotalPrice = 0;
            if (CollUtil.isNotEmpty(orderInfoList)) {
                totalPrice = orderInfoList.stream().filter(orderInfo -> orderInfo.getStatus()==40 || orderInfo.getStatus()==30)
                        .mapToInt(info -> ObjectUtil.defaultIfNull(info.getOrderPrice(), 0)).sum();
                extTotalPrice = orderInfoList.stream().filter(orderInfo -> orderInfo.getStatus()==40 || orderInfo.getStatus()==30)
                        .mapToInt(info -> ObjectUtil.defaultIfNull(info.getExtTotalPrice(), 0)).sum();
            }
            int total = (totalPrice + extTotalPrice) / 100;
            int maxValue = 500000;
            RankingListDTO ranking = new RankingListDTO();
            ranking.setName(citiesServed.getCityName());
            ranking.setMaxValue(maxValue);
            ranking.setValue(total);
            ranking.setPercentage((Math.floorDiv(total, maxValue)) + "%");
            rankingList.add(ranking);
        });
        return rankingList;
    }
}
